package com.jsp.web;

import com.jsp.mapper.UserMapper;
import com.jsp.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/QueryServlet")
public class QueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        /**
//         * 查询所有
//         * 1.sql：select * from tb_brand;
//         * 2.参数：；无
//         * 3.结果：List<Brand></Brand>
//         */
//
//
//
//        //1.定义 Connection
////        //1.导入jar包
////        //
////        //        //2.定义配置文件
////        //
////        //
////        //3.加载配置文件
//        Properties prop = new Properties();
//        prop.load(new FileInputStream("src/jdbc.properties"));
////
////        //4.获取连接对象
//        DataSource dataSource = null;
//        try {
//            dataSource = DruidDataSourceFactory.createDataSource(prop);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
////
//        Connection conn = null;
//        try {
//            conn = dataSource.getConnection();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
////
//////        System.out.println(conn);
//        //2.定义sql
//        String sql = "select * from tb_brand;";
//
//        //3.获取pstmt对象
//        PreparedStatement pstmt = null;
//        try {
//            pstmt = conn.prepareStatement(sql);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//        //4.设置参数
//
//        //5.执行sql
//        ResultSet rs = null;
//        try {
//            rs = pstmt.executeQuery();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//        List<Brand> brands = new ArrayList<Brand>();
//        Brand brand1;
//        //6.处理结果 List<Brand>,封装Brand对象，装载List集合
//        try {
//            while(rs.next()){
//                int id = rs.getInt("id");
//                String brandName = rs.getString("brand_name");
//                String companyName = rs.getString("company_name");
//                int ordered = rs.getInt("ordered");
//                String description = rs.getString("description");
//                int status = rs.getInt("status");
//                //封装Brand对象
//                brand1 = new Brand();
//                brand1.setBrandName(brandName);
//                brand1.setCompanyName(companyName);
//                brand1.setDescription(description);
//                brand1.setId(id);
//                brand1.setOrdered(ordered);
//                brand1.setStatus(status);
//                //封装集合
//                brands.add(brand1);
//
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
////    System.out.println(brands);
//        request.setAttribute("brands", brands);
//        request.setAttribute("rs", rs);
//        request.setAttribute("pstmt", pstmt);
//        request.setAttribute("conn", conn);
//        request.getRequestDispatcher("/index.jsp").forward(request,response);


        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Brand> brands = mapper.select();
        request.setAttribute("brands", brands);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
